import canmatrix.cli.convert
from openpyxl import load_workbook
from canmatrix import *
from canlib import kvadblib

import decimal
import cantools

## 读取excel表格
excel_file = "./BEV4.5T_PCAN_DYXYFQBEVTXV2000-20211016改PDU报文周期.xlsx"
sheet_name = "can_matrix"
workbook = load_workbook(excel_file)
sheet = workbook.worksheets[0]

# 打印第一行单元格内容
# for col in ['A', 'B', 'C']:
#     cell_value = sheet[f'{col}1'].value.replace('\n',' ')
#     print(f"单元格 {col}1: {cell_value}")




# 创建DBC实例
# matrix_obj = CanMatrix()

# # 总线类型
# matrix_obj.add_global_defines("BusType", "STRING")
# matrix_obj.add_define_default("BusType", "CAN")

# frame_id = 0x0CF103D0
# frame_name = "VCU_ControlMcuCmd"
# DLC = 8

# extended_id = ArbitrationId(frame_id, extended=True)


# new_frame = Frame(
#     arbitration_id=extended_id,
#     name=frame_name,
#     size=DLC
# )

# # matrix_obj.add_frame(new_frame)

# signal1 = Signal(
#     name="VCU_MCU_En",
#     start_bit=0,
#     size=1,
#     is_signed=0,
#     factor=1.0, offset=0, min=0, max=1,
#     unit="电机控制使能"
# )

# new_frame.add_signal(signal1)
# matrix_obj.add_frame(new_frame)

# # print(matrix_obj.frames)





# dbc = cantools.db.Database()
# message = cantools.db.Message(
#     frame_id=frame_id,
#     name=frame_name,
#     length=8,
#     signals=[
#         cantools.db.Signal(
#             name="VCU_MCU_En",
#             start=0,
#             length=1,
#             is_signed=0,
#             minimum=0,
#             maximum=1,
#             unit="电机控制使能"
#         )
#     ],
#     cycle_time=10,
# )


db = kvadblib.Dbc("./BEV4.5T（广州大运）.dbc")

db.write_file("./output.dbc")
db.close



